﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using TwainDotNet;
using TwainDotNet.WinFroms;
using ZXing;
using System.IO;

namespace AscentExpress
{
    public partial class MainForm : Form
    {
        Twain _twain;
        ScanSettings _settings;

        public MainForm()
        {
            InitializeComponent();
            _twain = new Twain(new WinFormsWindowMessageHook(this));
            _twain.TransferImage += (sender, args) =>
            {
                if (args.Image != null)
                {
                    pictureBox1.Image = args.Image;
                    // create a barcode reader instance
                    IBarcodeReader reader = new BarcodeReader();                    
                    // load a bitmap
                    //var barcodeBitmap = (Bitmap)Bitmap.LoadFrom("C:\\sample-barcode-image.png");
                    // detect and decode the barcode inside the bitmap
                    var result = reader.Decode(args.Image);
                    // do something with the result
                    if (result == null)
                    {
                        reader.TryHarder = true;
                        result = reader.Decode(args.Image);
                    }
                    string fileName = string.Empty;
                    if (result != null)
                    {
                        string barcodetype = result.BarcodeFormat.ToString();
                        string barcode = result.Text;
                        fileName = barcode;
                    }
                    else
                    {
                        fileName = Guid.NewGuid().ToString("N");
                    }
                    try
                    {
                        if (!Directory.Exists("images"))
                            Directory.CreateDirectory("images");
                        Kaliko.ImageLibrary.KalikoImage kalikoImage = new Kaliko.ImageLibrary.KalikoImage(args.Image);
                        //kalikoImage.WriteText("www.ascentcontent.com", args.Image.Width / 2, args.Image.Height / 2);
                        kalikoImage.SaveJpg(String.Format("images\\{0}.jpg", fileName), 90);
                    }
                    catch(Exception ex) { }
                }
            }; 
            _twain.ScanningComplete += (sender, e) =>
            {
                Enabled = true;
            };
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            StartScanning();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            ShowTwainUI();
        }
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            SelectSource();
        }

        private void SelectSource()
        {
            _twain.SelectSource();
        }

        private void ShowTwainUI()
        {
            try
            {
                _twain.StartScanning(new ScanSettings() { ShowTwainUI = true});
            }
            catch (TwainException ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void StartScanning()
        {
            //_settings = new ScanSettings
            //{
            //    UseDocumentFeeder = useAdfCheckBox.Checked,
            //    ShowTwainUI = useUICheckBox.Checked,
            //    ShowProgressIndicatorUI = showProgressIndicatorUICheckBox.Checked,
            //    UseDuplex = useDuplexCheckBox.Checked,
            //    Resolution =
            //        blackAndWhiteCheckBox.Checked
            //        ? ResolutionSettings.Fax : ResolutionSettings.ColourPhotocopier,
            //    Area = !checkBoxArea.Checked ? null : AreaSettings,
            //    Rotation = new RotationSettings
            //    {
            //        AutomaticRotate = autoRotateCheckBox.Checked,
            //        AutomaticBorderDetection = autoDetectBorderCheckBox.Checked
            //    }
            //};
            try
            {
                _twain.StartScanning(new ScanSettings() { ShowProgressIndicatorUI=false });
            }
            catch (TwainException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
